﻿using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using Abs.Core.Models.Blog;
using Abs.Core.DAL.EntityFramework;

namespace Abs.App.Controllers
{ 
    public class CommentController : Controller
    {
        private AbsContext db = new AbsContext();

        //
        // GET: /Comment/

        public ActionResult Index()
        {
            if (this.Session["Email"] == null)
            {
                return Redirect("~/User/Unauthorized_Access.aspx");
            }

            var comments = db.Comments.Include(c => c.User).Include(c => c.Post);
            return View(comments.ToList());
        }

        //
        // GET: /Comment/Details/5

        public ActionResult Details(int id)
        {
            if (this.Session["Email"] == null)
            {
                return Redirect("~/User/Unauthorized_Access.aspx");
            }

            Comment comment = db.Comments.Find(id);

            if (comment == null)
                return RedirectToAction("Index");

            return View(comment);
        }

        //
        // GET: /Comment/Create

        public ActionResult Create(int postId)
        {
            if (this.Session["Email"] == null)
            {
                return Redirect("~/User/Unauthorized_Access.aspx");
            }

            ViewBag.UserEmail = new SelectList(db.Users, "Email", "Name");
            ViewBag.PostId = new SelectList(db.Posts, "PostId", "UserEmail");
            return View(new Comment{PostId = postId});
        } 

        //
        // POST: /Comment/Create

        [HttpPost]
        public ActionResult Create(Comment comment)
        {
            if (this.Session["Email"] == null)
            {
                return Redirect("~/User/Unauthorized_Access.aspx");
            }

            comment.UserEmail = (string) this.Session["Email"];
            comment.Date = DateTime.Now;

            if (ModelState.IsValid)
            {
                db.Comments.Add(comment);
                db.SaveChanges();
                return this.RedirectToAction("Details", "Post", new {id = comment.PostId});
            }

            ViewBag.UserEmail = new SelectList(db.Users, "Email", "Name", comment.UserEmail);
            ViewBag.PostId = new SelectList(db.Posts, "PostId", "UserEmail", comment.PostId);
            return View(comment);
        }
        
        //
        // GET: /Comment/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (this.Session["Email"] == null)
            {
                return Redirect("~/User/Unauthorized_Access.aspx");
            }
            
            Comment comment = db.Comments.Find(id);

            if (comment == null)
                return RedirectToAction("Index");

            if ((string)this.Session["Email"] != comment.UserEmail &&
                (int)this.Session["Role"] != (int)Abs.Core.Models.User.RoleEnum.Admin)
                return this.Redirect("~/User/Unauthorized_Access.aspx");

            ViewBag.UserEmail = new SelectList(db.Users, "Email", "Name", comment.UserEmail);
            ViewBag.PostId = new SelectList(db.Posts, "PostId", "UserEmail", comment.PostId);
            return View(comment);
        }

        //
        // POST: /Comment/Edit/5

        [HttpPost]
        public ActionResult Edit(Comment comment)
        {
            if (this.Session["Email"] == null)
            {
                return Redirect("~/User/Unauthorized_Access.aspx");
            }

            if ((string)this.Session["Email"] != comment.UserEmail &&
                (int)this.Session["Role"] != (int)Abs.Core.Models.User.RoleEnum.Admin)
                return this.Redirect("~/User/Unauthorized_Access.aspx");

            if (ModelState.IsValid)
            {
                db.Entry(comment).State = EntityState.Modified;
                db.SaveChanges();
                return this.RedirectToAction("Details", "Post", comment.PostId);
            }
            ViewBag.UserEmail = new SelectList(db.Users, "Email", "Name", comment.UserEmail);
            ViewBag.PostId = new SelectList(db.Posts, "PostId", "UserEmail", comment.PostId);
            return View(comment);
        }

        //
        // GET: /Comment/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (this.Session["Email"] == null || (int)this.Session["Role"] != (int)Abs.Core.Models.User.RoleEnum.Admin)
            {
                return this.Redirect("~/User/Unauthorized_Access.aspx");
            }
            Comment comment = db.Comments.Find(id);

            if (comment == null)
                return RedirectToAction("Index");

            return View(comment);
        }

        //
        // POST: /Comment/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Comment comment = db.Comments.Find(id);

            if (comment == null)
                return RedirectToAction("Index");

            db.Comments.Remove(comment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}